﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class UpShelfController : BaseController<rpsmdeatils>
    {
        //
        // GET: /UpShelf/

        public override BLL.BaseBll<rpsmdeatils> bll
        {
            get
            {
                return new RpsmdeatilsBll();
            }

        }
        public class ListUpShelfModel  // waresCode   rate  avgUpNum  waresName
        {
            public string waresCode;//商品编号
            public int rate;  //上架频次
            public int avgUpNum; //平均上架数量 
            public string waresName;//商品名称
        }

        public ActionResult GetNotice(int page, int rows, log model, string waresName, string date1, string date2, string sort, string order)
        {

            var listLogUpModel = new List<ListUpShelfModel>();

            var orderByList = new List<ListUpShelfModel>();
            Expression<Func<rpsmdeatils, bool>> condition1 = null;
            //1. 查询表里的状态为 4的 所有的信息         排序字段  avgUpNum     
            if (String.IsNullOrEmpty(waresName))
            {
                condition1 = x => x.rpsmdeatilstatus == 4; //上架成功的

                var listNotice = bll.Search().Where(condition1).GroupBy(x => x.waresCode).Select(x => new
                {
                    x.FirstOrDefault().waresCode,
                    x.FirstOrDefault().waresName,
                    UpNum = x.Sum(y => y.waresNumb),
                    rate = x.Count(),

                }).ToList();
                foreach (var item in listNotice)
                {
                    ListUpShelfModel logmodel = new ListUpShelfModel();
                    logmodel.avgUpNum = item.UpNum / item.rate; // 9/2 ==4
                    logmodel.waresCode = item.waresCode;
                    logmodel.waresName = item.waresName;
                    logmodel.rate = item.rate;
                    listLogUpModel.Add(logmodel);

                }
            }
            else  //根据 商品 名称模糊查询
            {
                condition1 = x => x.rpsmdeatilstatus == 4 && x.waresName.Contains(waresName); //上架成功的

                var listNotice = bll.Search().Where(condition1).GroupBy(x => x.waresCode).Select(x => new
                {
                    x.FirstOrDefault().waresCode,
                    x.FirstOrDefault().waresName,
                    UpNum = x.Sum(y => y.waresNumb),
                    rate = x.Count(),

                }).ToList();
                foreach (var item in listNotice)
                {
                    ListUpShelfModel logmodel = new ListUpShelfModel();
                    logmodel.avgUpNum = item.UpNum / item.rate; // 9/2 ==4
                    logmodel.waresCode = item.waresCode;
                    logmodel.waresName = item.waresName;
                    logmodel.rate = item.rate;
                    listLogUpModel.Add(logmodel);

                }
            }


            if (sort == "avgUpNum" && order == "desc")
            {
                orderByList = listLogUpModel.OrderByDescending(x => x.avgUpNum).ToPagedList(page, rows).ToList(); //pageNum 页码数    pagesize  每页显示几条数据
            }
            if (sort == "avgUpNum" && order == "asc")
            {
                orderByList = listLogUpModel.OrderBy(x => x.avgUpNum).ToPagedList(page, rows).ToList(); //pageNum 页码数    pagesize  每页显示几条数据
            }
            if (sort == "rate" && order == "desc")
            {
                orderByList = listLogUpModel.OrderByDescending(x => x.rate).ToPagedList(page, rows).ToList();
            }
            if (sort == "rate" && order == "asc")
            {
                orderByList = listLogUpModel.OrderBy(x => x.rate).ToPagedList(page, rows).ToList();
            }
            return Json(new { rows = orderByList, total = listLogUpModel.Count() });
        }


    }
}
